package com.be.beadmin.device.controller;


import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.device.dto.input.InspectPlanDto;
import com.be.beadmin.device.dto.output.InspectTaskEx;
import org.springframework.data.domain.Page;
import com.be.beadmin.device.service.InspectTaskService;
import com.be.beadmin.device.dto.input.InspectTaskQueryCriteria;
import com.be.beadmin.device.dto.input.InspectTaskDto;
import org.springframework.data.domain.Pageable;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.List;
import com.alibaba.excel.EasyExcel;

/**
 * @author chenxu
 * @date 2023-08-03
 **/
@RestController
public class InspectTaskController {

    @Autowired
    private  InspectTaskService inspectTaskService;


    @PostMapping(value = "/device/getInspectTaskByPage")
    public ResultModel getInspectTaskByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(inspectTaskService.getInspectTaskByPage(pageEntity)) ;
    }

    @PostMapping("/device/addInspectTask")
    public ResultModel addInspectTask(@RequestBody InspectTaskDto inspectTaskDto){
        inspectTaskService.addInspectTask(inspectTaskDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/device/delInspectTask")
    public ResultModel delInspectTask(@RequestBody InspectTaskDto inspectTaskDto){
        inspectTaskService.delInspectTask(inspectTaskDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/device/updateInspectTask")
    public ResultModel updateInspectTask(@RequestBody InspectTaskDto inspectTaskDto){
        inspectTaskService.updateInspectTask(inspectTaskDto);
        return ResultModel.success(ResultModel.UPDATE);
    }
    @PostMapping("/device/generateInspectTask")
    public ResultModel generateInspectTask(@RequestBody PageEntity pageEntity){
        return ResultModel.success(inspectTaskService.generateInspectTask(pageEntity));
    }
    @PostMapping("/device/closeInspectTask")
    public ResultModel closeInspectTask(@RequestBody InspectTaskDto inspectTaskDto){
        return ResultModel.success(inspectTaskService.closeInspectTask(inspectTaskDto));
    }

    @PostMapping("/device/exportInspectTask")
    public void exportInspectTask(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<InspectTaskEx> inspectTaskPage = inspectTaskService.getInspectTaskByPage(pageEntity);
        List<InspectTaskEx> inspectTaskExes = inspectTaskPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("InspectTask接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), InspectTaskEx.class).sheet("sheet1").doWrite(inspectTaskExes);
    }
}